package com.yyx.lgb.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyx.lgb.model.dto.h5.ProductSkuDto;
import com.yyx.lgb.model.entity.product.Product;
import com.yyx.lgb.model.entity.product.ProductDetails;
import com.yyx.lgb.model.entity.product.ProductSku;
import com.yyx.lgb.model.vo.h5.ProductItemVo;
import com.yyx.lgb.product.mapper.ProductDetailsMapper;
import com.yyx.lgb.product.mapper.ProductMapper;
import com.yyx.lgb.product.mapper.ProductSkuMapper;
import com.yyx.lgb.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ProductServiceImpl
 * Package: com.yyx.lgb.product.service.impl
 * Description:
 *
 * @Author: yyx
 * @Create: 2025/8/11 - 14:15
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List<ProductSku> findProductSkuBySale() {
        return productSkuMapper.findProductSkuBySale();
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
       List<ProductSku> productSkuList= productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    @Override
    public ProductItemVo item(Long skuId) {

        //创建vo对象 用于封装数据
        ProductItemVo productItemVo = new ProductItemVo();
        //根据skuId获取sku信息
       ProductSku productSku= productSkuMapper.getById(skuId);
       //根据第二步的sku获取商品信息
       Product product= productMapper.getById(productSku.getProductId());
       //获取商品详细信息
        ProductDetails productDetails=productDetailsMapper.getByProductId(productSku.getProductId());
        //5 封装map集合==商品规格对应商品skuId信息
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        //根据商品id获取商品所有sku列表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productSku.getProductId());
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        //6 把需要数据封装到productItemVo里面
        productItemVo.setProduct(product);
        productItemVo.setProductSku(productSku);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        //封装详情图片list集合
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        //封装轮播图list集合
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        return productItemVo;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.getById(skuId);
    }
}
